package com.xue.apiautoframework.testcase;
import com.xue.apiautoframework.basecase.BaseCase;
import com.xue.apiautoframework.constant.*;
import com.xue.apiautoframework.mapper.RankMapper;
import com.xue.apiautoframework.mapper.UserMapper;
import com.xue.apiautoframework.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.testng.annotations.*;

import java.util.*;

@Slf4j
@SpringBootTest
public class RankCase extends BaseCase {

    @Autowired
    RankMapper rankMapper;

    @Test(dataProvider = "addData",description = "添加职级用例")
    public void testAddRank(API api, Cases cases){
        ResponseEntity<?> response = execute(api, cases);
    }

    @Test(dataProvider = "updateData", description = "修改职级用例")
    public void testUpdateRank(API api,  Cases  cases){
        String params = cases.getParams();
        Map<String, Object> strMap = JsonUtils.jsonToMap(params);
        try {
            assert strMap != null;
            strMap.put("id", rankMapper.getRankByName("123").getId()); // 查不到数据会抛异常
            params = JsonUtils.mapToJson(strMap);
            ResponseEntity<String> response = RestTemplateUtils.postForJson(api.getApiUrl(), super.map, params, String.class);
            execute(response, cases);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test(dataProvider = "deleteData", description = "删除职级用例")
    public void testDeleteRank(API api,  Cases  cases){
        Map<String, Object> strMap = new HashMap<>();
        strMap.put("id", rankMapper.getRankByName("321").getId());
        String params = JsonUtils.mapToJson(strMap);
        ResponseEntity<String> response = RestTemplateUtils.postForJson(api.getApiUrl(), super.map, params, String.class);
        execute(response, cases);
    }

    @DataProvider(name = "addData")
    public Object[][] providerAddData(){
        return ExcelUtils.getApiAndCasesByApiId("1");
    }

    @DataProvider(name = "updateData")
    public Object[][] providerUpdateData(){
        return ExcelUtils.getApiAndCasesByApiId("2");
    }

    @DataProvider(name = "deleteData")
    public Object[][] providerDeleteData(){
        return ExcelUtils.getApiAndCasesByApiId("3");
    }

}
